<?php
namespace app\loginEd\middle;
use app\common\util\Jwt;
use think\wenhainan\Auth;

class AuthMid
{
    public function handle($request,\Closure $next){
        $utk=request()->header('utk');
        if (!$utk) return R(false,[],"utk不存在");

        //检查jwt是否在缓存里, utk=jwt
        $dataByUtk = Jwt::getDataByUtk($utk);
        if (!$dataByUtk)return R(false,[],"登录超时");
        $u_r_g_a=json_decode($dataByUtk,true);
        $request->u_r_g_a = $u_r_g_a;
        //检查权限
        $auth = Auth::instance();
        $authCheckRs=$auth->check("index-",$u_r_g_a["uid"],config("auth.auth_type"));
        if (!$authCheckRs)return R(false,[],"没有权限");
        return $next($request);
    }
}